define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'retrieval/retrieval/index',
                    // add_url: 'retrieval/retrieval/add',
                    // edit_url: 'retrieval/retrieval/edit',
                    addcollect_url: 'my/article/addcollect',
                    detail_url: 'department/article/detail',
                    // del_url: 'retrieval/retrieval/del',
                    // multi_url: 'retrieval/retrieval/multi',
                    articlefile_url: 'my/article_file/index',
                    download_url: 'my/article_file/download',
                    offshelf_url: 'department/article/offshelf',//下架
                    table: 'my',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('ID'), operate: false, width: "70px", sortable: true},
                        {field: 'title', title: __('名称'), sortable: true, operate: 'LIKE', align: 'left',
                            formatter: function (value, row, index, field) {
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.content + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "250px"
                                    }
                                }
                            },},
                        {field: 'author', title: __('作者'), width: "150px", operate: 'LIKE', formatter: Table.api.formatter.label},
                        {field: 'first_author', title: __('负责人'), width: "150px", operate: 'LIKE', visible:false},
                        {field: 'introduction', title: __('摘要'), width: "150px", operate: 'LIKE', visible:false},
                        {field: 'parent_project', title: __('母项目'), width: "150px", operate: 'LIKE', visible:false},
                        {field: 'keywords', title: __('关键词'), width: "150px", operate: 'LIKE', visible:false},
                        {field: 'auth_department', title: __('作者部门'), width: "150px", operate: 'LIKE', visible:false},
                        {field: 'department_name', title: __('部门'), width: "150px", formatter: Table.api.formatter.label, operate: false},
                        {field: 'collect_num', title: __('收藏量'), width: "80px", sortable: true, operate: 'BETWEEN', formatter: Table.api.formatter.normal},
                        {
                            field: 'department_id',
                            title: __('部门'),
                            visible: false,
                            addclass: 'selectpage',
                            extend: 'data-source="department/index/selectpage" data-field="name"',
                            operate: 'in',
                            formatter: Table.api.formatter.search
                        },

                        // {field: 'id', title: __('Id'), sortable: true},
                        // {field: 'grouping.name', title: __('项目组'), operate: 'LIKE', formatter: Table.api.formatter.label},
                        {field: 'down_num', title: __('下载量'), operate: 'BETWEEN', sortable: true},
                        {field: 'view_num', title: __('浏览量'), operate: 'BETWEEN', sortable: true},
                        {field: 'content', title: __('全文摘要'), operate: "LIKE", visible: false},
                        {field: 'publish_time', title: __('发布日期'), width: "120px", formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true, datetimeFormat:"YYYY-MM-DD"},
                        {field: 'file_content', title: __('全文检索'), operate: 'like', visible: false},
                        {field: 'operate', title: __('Operate'), width: "150px", table: table, events: Table.api.events.operate,buttons: [
                                {
                                    name: 'addcollect',
                                    text: __('收藏'),
                                    title: __('收藏'),
                                    confirm: '确认收藏？',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: function (row) {
                                        return $.fn.bootstrapTable.defaults.extend.addcollect_url + "?ids=" + row.id + "&name=" + row.title;
                                    },
                                    success:function (data, ret) {
                                        if (ret.code == 1) {
                                            table.bootstrapTable('refresh');
                                        }

                                    }
                                },
                                {
                                    name: 'articlefile',
                                    text: __('附件'),
                                    title: __('附件'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return $.fn.bootstrapTable.defaults.extend.articlefile_url + "?article_id=" + row.id;
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                                },
                            ], formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function(params) {
                    var filter = JSON.parse(params.filter);
                    if (filter.file_content) {
                        params.file_content = filter.file_content;
                        delete filter.file_content;
                    }
                    params.filter = JSON.stringify(filter);
                    return params;
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", ".btn-download", function () {
                var ids =  Table.api.selectedids(table); //取出来是个数组  示例：[1,2,3,4,5]
                ids = ids.join(','); //用逗号隔开  数组分割后：1,2,3,4,5
                var url = $.fn.bootstrapTable.defaults.extend.download_url + "?article_id=" + ids;
                Fast.api.open(url, "下载");
            });

            $(document).on("click", ".btn-deletes", function(){
                Layer.confirm(__('确认删除这些吗？'), {
                    title: __('提示'),
                    btn: [__('确认'), __('取消')]
                }, function (index) {
                    let ids = Table.api.selectedids(table);
                    ids = ids.join(",");
                    var url = $.fn.bootstrapTable.defaults.extend.offshelf_url + "?ids=" + ids;
                    Fast.api.ajax({
                        url: url,
                        // data: {ids: ids},
                        loading: true
                    }, function (data, ret) {
                        table.bootstrapTable('refresh', {});
                        Toastr.success("操作成功");
                        return false;
                    }, function (data, ret) {
                        Toastr.error('操作失败');
                        return false;
                    });
                    layer.close(index);
                }, function (index) {
                    layer.close(index);
                });

            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});